package cn.action.product;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.poi.poifs.property.Parent;

import cn.action.BaseAction;
import cn.beans.Product;
import cn.beans.ProductCategory;
import cn.beans.ProductFile;
import cn.utils.PageModel;
import cn.utils.StringUtil;

/**
 * 前台产品展示action
 * @author Administrator
 *
 */
public class ProductShowAction extends BaseAction {
	
	private int pageSize=10;
	private  int currentPage=1;
	private int totalPage;
	private String selectId;
	private String subId;
	private String categoryId;
	private String pinpai="";
	private String price;
	private String subName;
	private String id;
	private int isDep=0;
	private List<ProductCategory> listParentCatogary=new ArrayList<ProductCategory>();
	private List<ProductCategory> listSubCatogary=new ArrayList<ProductCategory>();
	private HashSet<String> listPinpai=new HashSet<String>();
	private String sort;
	private PageModel pm;
	private Product product;
	private List<ProductFile >  listImg;
	private String title="";
	private String offertime;
	private String isHot;
	private String isNew;
	private String ispromote;
	private String kinds;
	//返回前台的跳转菜单
	private String jumpselect = "";
	private String keyword;
 	
	/**
	 * 跳转至产品列表
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if(!StringUtil.isAllEmpty(id)){
			ProductCategory category=commonservice.findById(id, new ProductCategory());
			if(null!=category){
				//判断低级类目的级别
				if(category.getLevels()==1){
					isDep=category.getIsIndependent();
					if(isDep==1){
						pinpai=category.getNameCn();
					}
				    selectId=category.getId();

				}else{
					isDep=0;
					subName=category.getNameCn();
					subId=category.getId();
					selectId=category.getParentId();
				}  
			}
		}
		
		//产出一级分类
		String hqlParent= "FROM ProductCategory o where o.levels=1 ORDER BY o.turnnumber asc ";
		listParentCatogary=super.findSomeByHQL.findDataByHql(hqlParent);
		getCatogary(selectId);
		
		return SUCCESS;
	}


	

	
	//查出产品列表
	public String listProduct(){
		PageModel model=new PageModel();
		model.setCurrentPage(currentPage);
		model.setPageSize(pageSize);
		
		pm=commonservice.findByPage(assHql(), model);
		
		if(null!=pm){
			int total=pm.getTotalCount();
			int totalPage=0;
			if(pm.getTotalCount()%pageSize==0){
				totalPage=pm.getTotalCount()/pageSize;
			}else{
				totalPage=pm.getTotalCount()/pageSize+1;
			}
			pm.setTotalPage(totalPage);
		}
		
		//返回前台的跳转菜单
		for(int i = 1 ; i < pm.getTotalPage()+1 ; i++){
			if(i==currentPage){
				jumpselect = jumpselect+"<option value='"+i+"' selected='selected' >第"+i+"页</option>";
			}else{
				jumpselect = jumpselect+"<option value='"+i+"'>第"+i+"页</option>";	
			}
		}
		
		return SUCCESS;
	}
	
	/**
	 * ajax加载二级分类和品牌
	 * @throws IOException 
	 */
	
	public void getSubCatogary() throws IOException{
		if(!StringUtil.isAllEmpty(selectId)){
			ProductCategory category=commonservice.findById(selectId,new ProductCategory() );
			isDep=category.getIsIndependent();
			if(isDep==1){
				pinpai=category.getNameCn();
			}
			getCatogary(selectId);
		}
		List list=new ArrayList();
		list.add(isDep);
		list.add(listPinpai);
		list.add(listSubCatogary);
		list.add(pinpai);
		JSONArray array=JSONArray.fromObject(list);
		super.sendMsgAjax(array.toString(), "utf-8");
	}
	
	/**
	 * 查看产品详情
	 * @return
	 */
	public String view(){
		
		try {
			product=commonservice.findById(id, new Product());
			product.setPv(product.getPv()+1);
			commonservice.updateDemo(product);
			String hqlImg="from ProductFile o where o.product.id='"+id+"' and o.mainPic <> '1' ";
			listImg=findSomeByHQL.findDataByHql(hqlImg);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	/**
	 * 列出热门产品
	 * @return
	 */
	public String listHot(){
		PageModel model=new PageModel();
		model.setCurrentPage(1);
		model.setPageSize(pageSize);
		String hql="from Product o order by o.pv desc ";
		pm=commonservice.findByPage(hql, model);
		return SUCCESS;
	}
	
	/**
	 * 找出品牌和二级分类
	 * @param id
	 */
	private void getCatogary(String thisid) {
		try {
			//if(1==isDep){
			//	listPinpai.add(pinpai);
			//}else{
				//查出二级类目
				String hqlSub="FROM ProductCategory o where o.levels=2 and o.parentId='"+thisid+"' ORDER BY o.turnnumber asc ";
				listSubCatogary=findSomeByHQL.findDataByHql(hqlSub);
				List<Product> listProduct=findSomeByHQL.findDataByHql(assHql());
				for(Product obj:listProduct){
					if(!StringUtil.isAllEmpty(obj.getBrand())){
						listPinpai.add(obj.getBrand());
					}
				}
			//}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 组装查询sql
	 * @return
	 */
	public String assHql(){
		String hql="from Product o where 1=1 ";
		if(!StringUtil.isAllEmpty(selectId,subId)){
			if(!StringUtil.isAllEmpty(subId)){
				hql=hql+" and  o.category.id='"+subId+"' ";
			}else{
				hql=hql+" and ( o.category.parentId='"+selectId+"' or o.category.id='"+selectId+"' )";
			}
		}
		
		if(!StringUtil.isAllEmpty(pinpai)){
			hql=hql+" and brand='"+pinpai+"'";
		}
		
		if(!StringUtil.isAllEmpty(price)){
			String strs[]=price.split("-");
			hql=hql+" and price >"+strs[0];
			if(strs.length>1){
				hql=hql+" and price <"+strs[1];
			}
		}
		
		if(!StringUtil.isAllEmpty(title)){
			hql=hql+" and (title like '%"+title+"%' or productid like '%"+title+"%' or bspecification like '%"+title+"%' or color  like '%"+title+"%')";
		}
		
		if(!StringUtil.isAllEmpty(offertime)){
			hql=hql+" and offertime = '"+offertime+"'";
		}
		
		if(!StringUtil.isAllEmpty(categoryId)){
			hql=hql+" and o.category.id = '"+categoryId+"'";
		}
		
		if(!StringUtil.isAllEmpty(keyword)){
			hql=hql+" and o.keyword like '%"+keyword+"%'";
		}
		
		if(!StringUtil.isAllEmpty(kinds)){
			if("1".equals(kinds)){
			hql=hql+" and o.isHot = '1'";
			}
			if("2".equals(kinds)){
				hql=hql+" and o.isNew = '1'";
			}
			if("3".equals(kinds)){
				hql=hql+" and o.ispromote = '1'";
			}			
		}
		
//		if(!StringUtil.isAllEmpty(isNew)){
//			hql=hql+" and o.isNew = '"+isNew+"'";
//		}
//		
//		if(!StringUtil.isAllEmpty(ispromote)){
//			hql=hql+" and o.ispromote = '"+ispromote+"'";
//		}	
		
		hql=hql+"  order by ";
		if(!StringUtil.isAllEmpty(sort)){
			hql=hql+" price "+sort+",";
		}
		
		hql=hql+" creDate desc";
		
		
		return hql;
	}
	/**
	 * 主页热门产品浏览
	 * @return
	 */
	public String homeHot(){
		
		String  hql="from Product o order by  o.ispromote desc , o.creDate desc";
		
		PageModel model=new PageModel();
		model.setCurrentPage(currentPage);
		model.setPageSize(pageSize);
		
		pm=commonservice.findByPage(hql, model);
		
		return SUCCESS;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public int getTotalPage() {
		return totalPage;
	}


	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}


	


	
	public String getSelectId() {
		return selectId;
	}





	public void setSelectId(String selectId) {
		this.selectId = selectId;
	}





	public String getSubId() {
		return subId;
	}


	public void setSubId(String subId) {
		this.subId = subId;
	}


	public String getCategoryId() {
		return categoryId;
	}


	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}


	



	public PageModel getPm() {
		return pm;
	}

	public void setPm(PageModel pm) {
		this.pm = pm;
	}

	public String getPinpai() {
		return pinpai;
	}


	public void setPinpai(String pinpai) {
		this.pinpai = pinpai;
	}


	public String getPrice() {
		return price;
	}


	public void setPrice(String price) {
		this.price = price;
	}


	public String getSubName() {
		return subName;
	}


	public void setSubName(String subName) {
		this.subName = subName;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public int getIsDep() {
		return isDep;
	}


	public void setIsDep(int isDep) {
		this.isDep = isDep;
	}


	


	public HashSet<String> getListPinpai() {
		return listPinpai;
	}


	public void setListPinpai(HashSet<String> listPinpai) {
		this.listPinpai = listPinpai;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}





	public List<ProductCategory> getListParentCatogary() {
		return listParentCatogary;
	}





	public void setListParentCatogary(List<ProductCategory> listParentCatogary) {
		this.listParentCatogary = listParentCatogary;
	}





	public List<ProductCategory> getListSubCatogary() {
		return listSubCatogary;
	}





	public void setListSubCatogary(List<ProductCategory> listSubCatogary) {
		this.listSubCatogary = listSubCatogary;
	}





	public Product getProduct() {
		return product;
	}





	public void setProduct(Product product) {
		this.product = product;
	}





	public List<ProductFile> getListImg() {
		return listImg;
	}





	public void setListImg(List<ProductFile> listImg) {
		this.listImg = listImg;
	}





	public String getTitle() {
		return title;
	}





	public void setTitle(String title) {
		this.title = title;
	}





	public String getOffertime() {
		return offertime;
	}





	public void setOffertime(String offertime) {
		this.offertime = offertime;
	}





	public String getIsHot() {
		return isHot;
	}





	public void setIsHot(String isHot) {
		this.isHot = isHot;
	}





	public String getIsNew() {
		return isNew;
	}





	public void setIsNew(String isNew) {
		this.isNew = isNew;
	}





	public String getIspromote() {
		return ispromote;
	}





	public void setIspromote(String ispromote) {
		this.ispromote = ispromote;
	}





	public String getKinds() {
		return kinds;
	}





	public void setKinds(String kinds) {
		this.kinds = kinds;
	}





	public String getJumpselect() {
		return jumpselect;
	}





	public void setJumpselect(String jumpselect) {
		this.jumpselect = jumpselect;
	}





	public String getKeyword() {
		return keyword;
	}





	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	
	

	
}
